package com.yixq.soa.service.web.service;


import com.yixq.soa.service.web.annotation.DataValid;
import com.yixq.soa.service.web.em.RegexType;
import com.yixq.soa.service.web.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

/**
 * Created by marvin on 2017/2/15.
 */
public class DataValidService {

    private static DataValid dataValid;

    public DataValidService() {
        super();
    }

    //解析的入口
    public static void valid(Object object) throws Exception{
        //获取object的类型
        Class<? extends Object> clazz=object.getClass();
        //获取该类型声明的成员
        Field[] fields=clazz.getDeclaredFields();
        //遍历属性
        for(Field field:fields){
            //对于private私有化的成员变量，通过setAccessible来修改器访问权限
            field.setAccessible(true);
            validate(field,object);
            //重新设置会私有权限
            field.setAccessible(false);
        }
    }


    public static void validate(Field field,Object object) throws Exception{

        String description;
        Object value;

        //获取对象的成员的注解信息
        dataValid=field.getAnnotation(DataValid.class);
        value=field.get(object);

        if(dataValid==null)return;

        description=dataValid.description().equals("")?field.getName():dataValid.description();

        /*************注解解析工作开始******************/
        if(!dataValid.nullable()){
            if(value==null|| StringUtils.isBlank(value.toString())){
                throw new Exception(description+"不能为空");
            }
            if(value.toString().length()>dataValid.maxLength()&&dataValid.maxLength()!=0){
                throw new Exception(description+"长度不能超过"+dataValid.maxLength());
            }

            if(value.toString().length()<dataValid.minLength()&&dataValid.minLength()!=0){
                throw new Exception(description+"长度不能小于"+dataValid.minLength());
            }
        }

        boolean nullFlag = dataValid.nullable();
        if(dataValid.regexType()!= RegexType.NONE){
            switch (dataValid.regexType()) {
                case NONE:
                    break;
                case NO_SPECIALCHAR:
                    if(!nullFlag && RegexUtils.hasSpecialChar(value.toString())){
                        throw new Exception(description+"不能含有特殊字符");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){//非必填且不为空
                        if(RegexUtils.hasSpecialChar(value.toString())){
                            throw new Exception(description+"不能含有特殊字符");
                        }
                    }
                    break;
                case IS_GUID:
                    if(!nullFlag && !RegexUtils.isGuid(value.toString())){
                        throw new Exception(description+"不能含有特殊字符");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isGuid(value.toString())){
                            throw new Exception(description+"不能含有特殊字符");
                        }
                    }
                    break;
                case CHINESE:
                    if(!nullFlag && !RegexUtils.isChinese2(value.toString())){
                        throw new Exception(description+"不是中文字符");
                    }
                    break;
                case EMAIL:
                    if(!nullFlag && !RegexUtils.isEmail(value.toString())){
                        throw new Exception(description+"地址格式不正确");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isEmail(value.toString())){
                            throw new Exception(description+"地址格式不正确");
                        }
                    }
                    break;
                case IP:
                    if(!nullFlag && !RegexUtils.isIp(value.toString())){
                        throw new Exception(description+"地址格式不正确");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isIp(value.toString())){
                            throw new Exception(description+"地址格式不正确");
                        }
                    }
                    break;
                case ID:
                    if(!nullFlag && !RegexUtils.isIdcard(value.toString())){
                        throw new Exception(description+"格式不正确");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isIdcard(value.toString())){
                            throw new Exception(description+"格式不正确");
                        }
                    }
                    break;
                case NUMBER:
                    if(!nullFlag && !RegexUtils.isNumber(value.toString())){
                        throw new Exception(description+"不是数字");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isNumber(value.toString())){
                            throw new Exception(description+"不是数字");
                        }
                    }
                    break;
                case MOBILE:
                    if(!nullFlag && !RegexUtils.isMobile(value.toString())){
                        throw new Exception(description+"格式不正确");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isMobile(value.toString())){
                            throw new Exception(description+"格式不正确");
                        }
                    }
                    break;
                case MOBILE_TEL:
                    if(!nullFlag && !RegexUtils.isMoibleOrTelePhone(value.toString())){
                        throw new Exception(description+"格式不正确");
                    }else if(nullFlag && RegexUtils.isNotNull(value)){
                        if(!RegexUtils.isMoibleOrTelePhone(value.toString())){
                            throw new Exception(description+"格式不正确");
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        if(!dataValid.regexExpression().equals("")){
            if(value.toString().matches(dataValid.regexExpression())){
                throw new Exception(description+"格式不正确");
            }
        }
        /*************注解解析工作结束******************/
    }
}
